package com.apexsoft.assets.base.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Component
@Slf4j
public class SsoUtil {
//    @Value("${application.sso.key}")
//    private static String key/* = "6Kf33GUQtVZw4Lj76NUHeb0PkDf1HjgE"*/;

    public static String buildWorkflowUrl(String userId, String ssoUrl, String key, String url) {
        if(url==null || url.isEmpty()) {
            return url;
        }
        String token = TokenUtils.buildToken(userId, key, false, "");
        try {
            url= URLEncoder.encode(url, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("buildWorkflowUrl ERROR: ", e);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(ssoUrl).append("/login.sso?UserID=").append(userId).append("&Token=").append(token).append("&aim=workflow&url=").append(url);
        return sb.toString();
    }
}
